﻿using System.Collections.Generic;
using System.Text;

namespace CSharp.Common.StringOperation
{
    public class StringPlus
    {
        public static string DelLastChar(string str, string strchar)
        {
            return str.Substring(0, str.LastIndexOf(strchar));
        }

        public static string DelLastComma(string str)
        {
            return str.Substring(0, str.LastIndexOf(","));
        }

        public static string GetArrayStr(List<string> list, string speater)
        {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < list.Count; i++)
            {
                if (i == (list.Count - 1))
                {
                    builder.Append(list[i]);
                }
                else
                {
                    builder.Append(list[i]);
                    builder.Append(speater);
                }
            }
            return builder.ToString();
        }

        public static string GetCleanStyle(string StrList, string SplitString)
        {
            if (StrList == null)
            {
                return "";
            }
            return StrList.Replace(SplitString, "");
        }

        public static string GetNewStyle(string StrList, string NewStyle, string SplitString, out string Error)
        {
            string str = "";
            if (StrList == null)
            {
                str = "";
                Error = "Invaid string";
                return str;
            }
            int length = StrList.Length;
            int num2 = GetCleanStyle(NewStyle, SplitString).Length;
            if (length != num2)
            {
                str = "";
                Error = "Invaid string";
                return str;
            }
            string str2 = "";
            for (int i = 0; i < NewStyle.Length; i++)
            {
                if (NewStyle.Substring(i, 1) == SplitString)
                {
                    str2 = str2 + "," + i;
                }
            }
            if (str2 != "")
            {
                str2 = str2.Substring(1);
            }
            string[] strArray = str2.Split(new char[] { ',' });
            foreach (string str3 in strArray)
            {
                StrList = StrList.Insert(int.Parse(str3), SplitString);
            }
            str = StrList;
            Error = "";
            return str;
        }

        public static string[] GetStrArray(string str)
        {
            return str.Split(new char[0x2c]);
        }

        public static List<string> GetStrArray(string str, char speater, bool toLower)
        {
            List<string> list = new List<string>();
            string[] strArray = str.Split(new char[] { speater });
            foreach (string str2 in strArray)
            {
                if (!string.IsNullOrEmpty(str2) && (str2 != speater.ToString()))
                {
                    string item = str2;
                    if (toLower)
                    {
                        item = str2.ToLower();
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public static List<string> GetSubStringList(string o_str, char sepeater)
        {
            List<string> list = new List<string>();
            string[] strArray = o_str.Split(new char[] { sepeater });
            foreach (string str in strArray)
            {
                if (!(string.IsNullOrEmpty(str) || !(str != sepeater.ToString())))
                {
                    list.Add(str);
                }
            }
            return list;
        }

        public static string ToDBC(string input)
        {
            char[] chArray = input.ToCharArray();
            for (int i = 0; i < chArray.Length; i++)
            {
                if (chArray[i] == '　')
                {
                    chArray[i] = ' ';
                }
                else if ((chArray[i] > 0xff00) && (chArray[i] < 0xff5f))
                {
                    chArray[i] = (char)(chArray[i] - 0xfee0);
                }
            }
            return new string(chArray);
        }

        public static string ToSBC(string input)
        {
            char[] chArray = input.ToCharArray();
            for (int i = 0; i < chArray.Length; i++)
            {
                if (chArray[i] == ' ')
                {
                    chArray[i] = '　';
                }
                else if (chArray[i] < '\x007f')
                {
                    chArray[i] = (char)(chArray[i] + 0xfee0);
                }
            }
            return new string(chArray);
        }
    }
}

